Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

Array.prototype.contain = function(val) {
    var r = false;
    this.forEach(function(v,index){
      if(val==v){
        r = true;
      }
    });
    return r;
};

Date.prototype.format = function(formatStr){
    var str = formatStr;
    str=str.replace(/yyyy|YYYY/,this.getFullYear());
    str=str.replace(/MM/,(this.getMonth()+1)>9?(this.getMonth()+1).toString():'0' + (this.getMonth()+1));
    str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
    return str;
}

logout = function($http){
    $http({method:'POST',url:'quant4j-service/authority/logout'}).success(function(data,header,config,status) {
      location.href="index.html";
    }).error(function(data,header,config,status){
      console.log(data);
    });
}

getUser = function($http,$scope,current,parent){
    $scope.haserror = false;
    $scope.errormessage = "";
    $http.get('quant4j-service/authority').success(function(data) {
      if(data.ok){
        $scope.user = data.data;
        var menus = [];
        angular.forEach($scope.user.menus, function(menu) {
          var m = {};
          m.label = menu.label;
          m.icon = menu.icon;
          var menuclass = '';
          if(current==m.label){
            menuclass += 'active';
            m.url = "#";
          }else{
            menuclass += '';
            m.url = menu.url;
          }
          if(menu.items.length>0){
            menuclass += ' sub ';
            if(menu.label == parent){
                menuclass += ' open active';
            }
          }else{
            menuclass += '';
          }
          m.menuclass = menuclass;
          var items = [];
          angular.forEach(menu.items,function(item){
            var i = {};
            i.label = item.label;
            i.url = item.url;
            items.push(i);
          });
          m.items = items;
          menus.push(m);
        });
        $scope.menus = menus;
        $scope.initAfter();
      }
    }).error(function(data,status,headers,config){
      $scope.haserror = true;
      $scope.errormessage = data.message;
    });
}

changemenuclass=function($scope,currentmenu){
    angular.forEach($scope.menus, function(menu) {
      if(menu.label == currentmenu.label){
         var newmenuclass = '';
         if(menu.menuclass.search('active')!=-1){
            newmenuclass+='active';
         }
         if(currentmenu.items.length>0){
             if(currentmenu.menuclass.search('sub open')!=-1){
                newmenuclass+=' sub ';
             }else{
                newmenuclass+=' sub open ';
             }
         }
         menu.menuclass = newmenuclass;
      }
    });
}

initPage = function($scope,size,maxpage){
  $scope.maxpage = maxpage;//最多10页
  $scope.size = size;//每页多少条
  $scope.pages = [];
  $scope.data = {
    content: [],
    totalElements: 0,
    last: false,
    totalPages: 0,
    number: 0,
    size: size,
    first: true,
  };
}

pagecommon = function($scope,data) {
    if(data.ok){
      $scope.data = data.data;
      if($scope.data.first){
        $scope.firstclass = "disabled";
      }else{
        $scope.firstclass = "";
      }
      if($scope.data.last){
        $scope.lastclass = "disabled";
      }else{
        $scope.lastclass = "";
      }
      $scope.pages = [];
      var a = data.data.number - $scope.maxpage/2;
      var start = a<0?0:a;
      for(var i=start;i<data.data.totalPages && i-start<$scope.maxpage;i++){
          var page = {};
          page.i = i;
          if(i==data.data.number){
            page.currentclass = "active";
          }else{
            page.currentclass = "";
          }
          $scope.pages.push(page);
      }
    }else{
      $scope.haserror = true;
      $scope.errormessage = data.message;
    }
}

ajaxerror=function(data,status,headers,config){
  alert('系统错误:'+status);
}